package com.zmn.oms.dubbo.impl.zmn.normal.warranty;

import org.apache.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.constant.OrderGlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.reflect.CopyObjectUtil;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.oms.dubbo.dto.zmn.warranty.WarrantyListUserDRO;
import com.zmn.oms.dubbo.dto.zmn.warranty.WarrantyUserDRO;
import com.zmn.oms.dubbo.interfaces.zmn.normal.warranty.ZsNormalWarrantyRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.warranty.WarrantyListUserVO;
import com.zmn.oms.model.vo.warranty.WarrantyUserVO;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.normal.business.interfaces.warranty.ZsNormalOrderWarrantyBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 类描述：保单 remote service
 *
 * @author heciqi
 * @date 2018/12/3 20:11
 */
@Deprecated
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ZsNormalWarrantyRemoteServiceImpl implements ZsNormalWarrantyRemoteService {


	private Logger logger = LoggerFactory.getLogger(ZsNormalWarrantyRemoteServiceImpl.class);

	private static final String TAG = "啄木鸟-普通工单-保修卡dubbo接口";

	@Autowired
	private ZsNormalOrderWarrantyBService zsNormalOrderWarrantyBService;
	@Autowired
	private OrderWorkService orderWorkService;

	/**
	 * 获取保修卡
	 *
	 * @param orderId
	 * @return
	 */
	@Override
	public ResponseDTO<WarrantyUserDRO> findWarrantyByOrderId(Long orderId) {
	    if (orderId == null) {
	        return ResponseUtil.responseFail("orderId 不为空");
        }

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, orderId);
	    if (orderWork == null) {
            return ResponseUtil.responseFail("订单不存在");
        }

        Integer status = orderWork.getStatus();
        if (status < OrderGlobalConsts.WORK_STATUS_COMPLETE) {
            return ResponseUtil.responseFail("订单未完成");
        }

        WarrantyUserVO warrantyVO = zsNormalOrderWarrantyBService.findWarrantyByOrderId(orderId);
		if (warrantyVO == null) {
			return ResponseUtil.responseFail();
		}
		WarrantyUserDRO userDRO = BeanMapper.map(warrantyVO, WarrantyUserDRO.class);
		return ResponseUtil.responseSuccess(userDRO, GlobalConsts.SUCCESS_MSG);
	}

	/**
	 * 保修卡
	 * @param userId
	 * @return
	 */
	@Override
	public ResponseDTO<WarrantyListUserDRO> listWarrantyByUserId(Long userId) {
		List<WarrantyListUserVO> warranties = zsNormalOrderWarrantyBService.listWarrantyByUserId(userId);
		logger.info("保修卡查询结果[{}]", warranties);
		// 列表转换
		List<WarrantyListUserDRO> warrantyDROS = Lists.newArrayList();
		if (!CollectionUtils.isEmpty(warranties)) {
			for (WarrantyListUserVO warranty : warranties) {
				WarrantyListUserDRO warrantyListUserDRO = new WarrantyListUserDRO();
				CopyObjectUtil<WarrantyListUserVO, WarrantyListUserDRO> copyObjectUtil = new CopyObjectUtil<>(warranty, warrantyListUserDRO);
				copyObjectUtil.copy();

				warrantyDROS.add(warrantyListUserDRO);
			}
		}

		return ResponseUtil.responseSuccess(warrantyDROS, "OK");
	}

	/**
	 * 激活保单
	 *
	 * @param orderId 用户id
	 * @return 激活结果
	 */
	@Override
	public ResponseDTO activeWarranty(Long orderId) {
		zsNormalOrderWarrantyBService.activeWarranty(orderId);
		return ResponseUtil.responseSuccess(null, "OK");
	}

	/**
	 * 领取保修卡
	 *
	 * @param orderId
	 * @param userId
	 * @return
	 */
	@Override
	public ResponseDTO takeWarranty(Long orderId, Long userId) {
		zsNormalOrderWarrantyBService.takeWarranty(orderId, userId);
		return ResponseUtil.responseSuccess(null, "OK");
	}
}
